package com.gsx.Controller;


import com.gsx.entity.UserInfo;
import com.gsx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RegisterController {
    @Autowired
    private UserMapper userMapper;

    /*注册*/
    @GetMapping("/register")
    public String toRegister(){
        return "register";
    }

    @PostMapping("/registerCompelete")
    public String newUser(UserInfo user, RedirectAttributes attributes){
        int result=userMapper.newUser(user);
        if(result==0){
            attributes.addFlashAttribute("message","用户名或者邮箱已经存在");
            return "redirect:/register";
        }else {
            return "login";
        }
    }

    /*更改密码*/

    @GetMapping("/forget")
    public String toForgetPassword(){
        return "forgetPassword";
    }

    @PostMapping("/forgetPassword")
    public String updatePassword(UserInfo user, RedirectAttributes attributes){
        int result=userMapper.updatePassword(user);
        if(result==0){
            attributes.addFlashAttribute("message","修改失败");
            return "redirect:/forget";
        }else {
            return "login";
        }
    }
}
